#ifndef __MAIN_H__
#define __MAIN_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <esp_log.h>
#include <time.h>
#include <math.h>
#include <driver/i2c.h>
#include <driver/ledc.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/spi_master.h>
#include <esp_event.h>
#include <nvs_flash.h>
#include <esp_system.h>
#include <lwip/sys.h>
#include <driver/temperature_sensor.h>
#include <freertos/portmacro.h>

#include "llist.h"
#include "timer.h"
//#include "buzzer.h"
#include "key.h"
#include "ina228.h"
#include "oled.h"
#include "font.h"
#include "i2c.h"

#define Printf(format,args...) do{\
        printf("[%s:%d]:"format,__ASSERT_FUNC,__LINE__,##args);\
    }while(0)

#define HISTORY_MAX 3600
#define HISTORY_DROP 360

typedef void(*view_show)(void);
typedef void(*menu_fun)(void);

typedef struct {
    float raw_mV;
    float raw_mA;
    float mV;
    float mA;
    float mW;
    int32_t max_mA;
    int32_t max_mW;
    int32_t mWH;
    int32_t mAH;
    int32_t mAh_tmp;
    int32_t mWh_tmp;
    float temp;
} measure_t;

typedef struct {
    int list[HISTORY_MAX];
    int len;
} history_t;

typedef struct {
    int32_t Count_mAh;
    int32_t Count_mWh;
	int32_t Count_Max_mA;
	int32_t Count_Max_mW;

} history_count;

typedef struct {
	uint32_t low_volt;		//max:85000
	uint32_t over_curt;		//max:99990
    uint8_t over_temp;		//max:80
    uint32_t res;			//max:99990
    uint32_t MAXcurrent;	//max:128000
    int8_t main;			//主页翻页，暂时0-2
    int8_t rotate;			//0-1
    int8_t contrast;		//0-2

    uint8_t Config_RST;				//0-1
    uint8_t Config_RSTACC;			//0-1
    uint8_t Config_CONVDLY;			//0-255
    uint8_t Config_TEMPCOMP;		//0-1
    uint8_t Config_ADCRANGE;		//0-1

    uint8_t ADC_ADCMode;			//0-15
    uint8_t ADC_VBUSCT;				//0-7
    uint8_t ADC_VSHCT;				//0-7
    uint8_t ADC_VTCT;				//0-7
    uint8_t ADC_ADCAVG;				//0-7
    uint16_t Config_TEMPCOMP_PPM;	//0-16383

} store_t;

typedef struct {
    struct {
        int raw;
        float val;
    } volt[4];  //5V 10V 15V 20V
    struct {
        int raw;
        float val;
    } curt[6];  //1A 2A 5A 10A 20A 50A
    int zero;
    //char reserved[32];
} calibration_t;

typedef struct {
    int menuStart;
    int menuIdx;
    int main_idx;
    int timeDiv;
    int seting;
    int set_idx;
    int cal_idx;
    history_count count;
    store_t store;
    calibration_t cal;
    int cal_point;
    view_show view;
    measure_t msr;
    history_t history;
    key_trigger_t keys[keyNumMax];
    struct timer_list key_timer;
    struct timer_list show_timer;
    struct timer_list elec_timer;
    struct timer_list ina228_timer;
    struct timer_list oled_timer;
} meter_var_t;

extern meter_var_t mvar;
extern const int volt_cal_point[];
extern const int curt_cal_point[];

void view_show_menu(void);
void show_alert(void);
void save(void);
void save_cal(void);
void save_count(void);

#endif

